package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.ActivityCommentDAO;
import com.wisewin.circle.dao.ActivityCommentHeartDAO;
import com.wisewin.circle.dao.ActivityDAO;
import com.wisewin.circle.dao.ActivitySignDAO;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.*;
import com.wisewin.circle.entity.dto.ActivitySignUserDTO;
import com.wisewin.circle.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

@Service
@Transactional
public class ActivityService {

    @Autowired
    private ActivityDAO activityDAO;
    @Autowired
    private ActivitySignDAO activitySignDAO;
    @Autowired
    private ActivityCommentDAO activityCommentDAO;
    @Autowired
    private ActivityCommentHeartDAO activityCommentHeartDAO;

    // 查询活动评论信息
    public ResultDTO queryActivityCommentList(QueryParamBO queryParamBO){
        if(queryParamBO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 判断活动是否存在
        ActivityDO activity = activityDAO.queryActivityDetail(queryParamBO.getActivityId());
        if(activity == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("activityId", queryParamBO.getActivityId());
        List<ActivityCommentDO> lstComment = activityCommentDAO.queryActivityCommentList(map);
        if(!CollectionUtils.isEmpty(lstComment)){
            for(ActivityCommentDO activityCommentDO : lstComment){
                String todayTime = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
                String yesterdayTime = DateUtil.formatDate(DateUtil.getBeforeDay(new Date(), -1), "yyyy-MM-dd");
                String beforYesterdayTime = DateUtil.formatDate(DateUtil.getBeforeDay(new Date(), -2), "yyyy-MM-dd");
                String createTime = DateUtil.formatDate(activityCommentDO.getCreateTime(), "yyyy-MM-dd");
                String commentTime = "";
                if(todayTime.equals(createTime)){
                    commentTime = "今天 " + DateUtil.formatDate(activityCommentDO.getCreateTime(), "HH:mm");
                }else if(yesterdayTime.equals(createTime)){
                    commentTime = "昨天 " + DateUtil.formatDate(activityCommentDO.getCreateTime(), "HH:mm");
                }else if(beforYesterdayTime.equals(createTime)){
                    commentTime = "前天 " + DateUtil.formatDate(activityCommentDO.getCreateTime(), "HH:mm");
                }else{
                    commentTime = DateUtil.formatDate(activityCommentDO.getCreateTime(), "yyyy-MM-dd HH:mm");;
                }
                activityCommentDO.setCommentTime(commentTime);
            }
        }
        return ResultDTOBuilder.success(lstComment);
    }

    // 取消活动评论点赞
    public ResultDTO cancleActivityCommentHeart(ActivityCommentHeartDO activityCommentHeartDO){
        if(activityCommentHeartDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        ActivityCommentDO activityCommentDO = activityCommentDAO.queryActivityCommentById(activityCommentHeartDO.getCommentId());
        if(activityCommentDO == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 判断是否已经点赞
        ActivityCommentHeartDO activityCommentHeartOld = activityCommentHeartDAO.queryActivityCommentHeartById(activityCommentHeartDO.getUserId(),
                activityCommentHeartDO.getCommentId());
        if(activityCommentHeartOld == null){
            return ResultDTOBuilder.failure("1", "未点赞", null);
        }
        // 删除评论点赞
        activityCommentHeartDAO.delete(activityCommentHeartDO.getUserId(), activityCommentHeartDO.getCommentId());
        // 修改评论点赞次数
        activityCommentDAO.cancleHeartNum(activityCommentHeartDO.getCommentId());
        return ResultDTOBuilder.success(activityCommentHeartDO);
    }

    // 添加活动评论点赞
    public ResultDTO addActivityCommentHeart(ActivityCommentHeartDO activityCommentHeartDO){
        if(activityCommentHeartDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        ActivityCommentDO activityCommentDO = activityCommentDAO.queryActivityCommentById(activityCommentHeartDO.getCommentId());
        if(activityCommentDO == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 判断是否已经点赞
        ActivityCommentHeartDO activityCommentHeartOld = activityCommentHeartDAO.queryActivityCommentHeartById(activityCommentHeartDO.getUserId(),
                activityCommentHeartDO.getCommentId());
        if(activityCommentHeartOld != null){
            return ResultDTOBuilder.failure("1", "已点赞", null);
        }
        // 添加评论点赞
        activityCommentHeartDAO.insert(activityCommentHeartDO);
        // 修改评论点赞次数
        activityCommentDAO.updateHeartNum(activityCommentHeartDO.getCommentId());
        return ResultDTOBuilder.success(activityCommentHeartDO);
    }

    // 删除活动评论
    public ResultDTO deleteActivityComment(ActivityCommentDO activityCommentDO){
        if(activityCommentDO.getCommentId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询评论是否存在
        ActivityCommentDO activityCommentOld = activityCommentDAO.queryActivityCommentById(activityCommentDO.getCommentId());
        if(activityCommentOld == null){
            return ResultDTOBuilder.failure("1", "当前评论不存在", null);
        }
        // 删除评论
        activityCommentDAO.delete(activityCommentDO.getCommentId());
        // 修改评论次数
        activityDAO.cancleCommentNum(activityCommentOld.getActivityId());
        return ResultDTOBuilder.success(activityCommentDO);
    }

    // 添加活动评论
    public ResultDTO addActivityComment(ActivityCommentDO activityCommentDO){
        Integer parentId = activityCommentDO.getParentId() == null ? 0 : activityCommentDO.getParentId();
        activityCommentDO.setParentId(parentId);
        if(activityCommentDO.getActivityId() == null || activityCommentDO.getAcceptUserId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        if(StringUtils.isEmpty(activityCommentDO.getContent())){
            return ResultDTOBuilder.failure("1", "请填写评论内容", null);
        }
        // 查询活动
        ActivityDO activityOld = activityDAO.queryActivityById(activityCommentDO.getActivityId());
        if(activityOld == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        // 添加活动评论信息
        activityCommentDAO.insert(activityCommentDO);
        // 添加活动评论数量
        activityDAO.updateCommentNum(activityCommentDO.getActivityId());
        return ResultDTOBuilder.success(activityCommentDO);
    }

    // 查询活动推荐列表
    public ResultDTO queryActivityRecommendList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(activityDAO.queryActivityCount(map), activityDAO.queryActivityList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(ActivityDO activityDO : (List<ActivityDO>)pageData.getRecords()){
            // 是否关注
            Integer ifSign = activityDO.getUserId() == queryParamBO.getUserId() ? 1 : activityDO.getSignId() == null ? 0 : 1;
            activityDO.setIfSign(ifSign);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询活动列表
    public ResultDTO queryActivityList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("typeId", queryParamBO.getTypeId());
        map.put("ifSelf", queryParamBO.getIfSelf());
        // 查询条件
        map.put("userName", queryParamBO.getUserName());
        map.put("phone", queryParamBO.getPhone());
        map.put("title", queryParamBO.getTitle());
        map.put("position", queryParamBO.getPosition());
        map.put("beginTime", queryParamBO.getBeginTime());
        map.put("endTime", queryParamBO.getEndTime());
        // 分页条件
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(activityDAO.queryActivityCount(map), activityDAO.queryActivityList(map), pageUtil);
        if (pageData == null || CollectionUtils.isEmpty(pageData.getRecords())) {
            return ResultDTOBuilder.success(pageData);
        }
        // 处理数据
        for(ActivityDO activityDO : (List<ActivityDO>)pageData.getRecords()){
            // 是否关注
            Integer ifSign = activityDO.getUserId() == queryParamBO.getUserId() ? 1 : activityDO.getSignId() == null ? 0 : 1;
            activityDO.setIfSign(ifSign);
        }
        return ResultDTOBuilder.success(pageData);
    }

    // 查询活动报名列表
    public ResultDTO queryActivitySignList(QueryParamBO queryParamBO, PageUtil pageUtil){
        if(queryParamBO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", queryParamBO.getUserId());
        map.put("activityId", queryParamBO.getActivityId());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(activitySignDAO.queryActivitySignCount(map), activitySignDAO.queryActivitySignList(map), pageUtil);
        return ResultDTOBuilder.success(pageData);
    }

    // 查询活动详细信息
    public ResultDTO queryActivityDetail(ActivityDO activityDO){
        if(activityDO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        ActivityDO activity = activityDAO.queryActivityDetail(activityDO.getActivityId());
        if(activity == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        // 处理活动信息
        String startTime = DateUtil.formatDate(activity.getStartTime(), "MM-dd HH:ss");
        String endTime = DateUtil.formatDate(activity.getEndTime(), "MM-dd HH:ss");
        activity.setStartTimeStr(startTime);
        activity.setEndTimeStr(endTime);
        // 处理是否参加 创建人默认参加
        ActivitySignDO activitySignDO = activitySignDAO.queryActivitySignById(activityDO.getActivityId(), activity.getUserId());
        Integer ifSign = activityDO.getUserId() == activity.getUserId() ? 1 : activitySignDO == null ? 0 : 1;
        activity.setIfSign(ifSign);
        // 是否可以取消 创建人不能取消
        Integer ifCanCancle = activityDO.getUserId() == activity.getUserId() ? 0 : 1;
        activity.setIfCanCancle(ifCanCancle);
        // 查询报名信息
        List<ActivitySignUserDTO> lstSignUser = activitySignDAO.queryActivitySignUser(activity.getActivityId());
        Map<String, Object> result = new HashMap<>();
        result.put("activity", activity);
        result.put("signUser", lstSignUser);
        return ResultDTOBuilder.success(result);
    }

    // 取消活动报名
    public ResultDTO cancleSignActivity(ActivityDO activityDO){
        if(activityDO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询活动
        ActivityDO activityOld = activityDAO.queryActivityById(activityDO.getActivityId());
        if(activityOld == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        // 判断是否报名活动
        ActivitySignDO activitySignOld = activitySignDAO.queryActivitySignById(activityDO.getActivityId(), activityDO.getUserId());
        if(activitySignOld == null){
            return ResultDTOBuilder.failure("1", "您未报名该活动", null);
        }
        // 删除活动报名
        activitySignDAO.delete(activityDO.getActivityId(), activityDO.getUserId());
        // 报名人数减少
        activityDAO.cancleSignNum(activityDO.getActivityId());
        return ResultDTOBuilder.success(activityDO);
    }

    // 活动报名
    public ResultDTO signActivity(ActivityDO activityDO){
        if(activityDO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询活动
        ActivityDO activityOld = activityDAO.queryActivityById(activityDO.getActivityId());
        if(activityOld == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        // 判断是否报名活动
        ActivitySignDO activitySignOld = activitySignDAO.queryActivitySignById(activityDO.getActivityId(), activityDO.getUserId());
        if(activitySignOld != null){
            return ResultDTOBuilder.failure("1", "您已经报名该活动", null);
        }
        // 添加报名信息
        ActivitySignDO activitySignDO = new ActivitySignDO();
        activitySignDO.setActivityId(activityDO.getActivityId());
        activitySignDO.setUserId(activityDO.getUserId());
        activitySignDAO.insert(activitySignDO);
        // 修改活动报名人数
        activityDAO.updateSignNum(activityDO.getActivityId());
        return ResultDTOBuilder.success(activitySignDO);
    }

    // 删除活动
    public ResultDTO deleteActivity(ActivityDO activityDO){
        if(activityDO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        ActivityDO activityOld = activityDAO.queryActivityById(activityDO.getActivityId());
        if(activityOld == null || activityOld.getStatus() == SysDictDO.STATUS_YSC){
            return ResultDTOBuilder.failure("1", "活动不存在或者已删除", null);
        }
        // 删除数据
        activityDAO.updateStatus(activityDO.getActivityId(), SysDictDO.STATUS_YSC);
        return ResultDTOBuilder.success(null);
    }

    // 修改活动信息
    public ResultDTO updateActivity(ActivityDO activityDO){
        if(activityDO.getActivityId() == null){
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询活动
        ActivityDO activityOld = activityDAO.queryActivityById(activityDO.getActivityId());
        if(activityOld == null){
            return ResultDTOBuilder.failure("1", "活动不存在", null);
        }
        // 修改活动
        if(activityDO.getTypeId() == null){
            return ResultDTOBuilder.failure("1", "请选择活动类型", null);
        }
        if(StringUtils.isEmpty(activityDO.getHeadUrl())){
            return ResultDTOBuilder.failure("1", "请上传活动图片", null);
        }
        if(StringUtils.isEmpty(activityDO.getTitle())){
            return ResultDTOBuilder.failure("1", "请填写活动标题", null);
        }
        if(StringUtils.isEmpty(activityDO.getStartTimeStr()) || StringUtils.isEmpty(activityDO.getEndTimeStr())){
            return ResultDTOBuilder.failure("1", "请填写活动开始时间和结束时间", null);
        }
        // 活动时间
        Date startTime = DateUtil.parseDate(activityDO.getStartTimeStr(), "yyyy-MM-dd HH:mm:ss");
        Date endTime = DateUtil.parseDate(activityDO.getEndTimeStr(), "yyyy-MM-dd HH:mm:ss");
        if(startTime.getTime() > endTime.getTime()){
            return ResultDTOBuilder.failure("1", "活动开始时间大于结束时间", null);
        }
        activityDO.setStartTime(startTime);
        activityDO.setEndTime(endTime);
        if(StringUtils.isEmpty(activityDO.getPosition())){
            return ResultDTOBuilder.failure("1", "请填写活动地点", null);
        }
        if(StringUtils.isEmpty(activityDO.getIntroduce())){
            return ResultDTOBuilder.failure("1", "请填写活动简介", null);
        }
        activityDAO.update(activityDO);
        return ResultDTOBuilder.success(null);
    }

    // 创建活动
    public ResultDTO addActivity(ActivityDO activityDO){
        if(activityDO.getTypeId() == null){
            return ResultDTOBuilder.failure("1", "请选择活动类型", null);
        }
        if(StringUtils.isEmpty(activityDO.getHeadUrl())){
            return ResultDTOBuilder.failure("1", "请上传活动图片", null);
        }
        if(StringUtils.isEmpty(activityDO.getTitle())){
            return ResultDTOBuilder.failure("1", "请填写活动标题", null);
        }
        if(StringUtils.isEmpty(activityDO.getStartTimeStr()) || StringUtils.isEmpty(activityDO.getEndTimeStr())){
            return ResultDTOBuilder.failure("1", "请填写活动开始时间和结束时间", null);
        }
        // 活动时间
        Date startTime = DateUtil.parseDate(activityDO.getStartTimeStr(), "yyyy-MM-dd HH:mm:ss");
        Date endTime = DateUtil.parseDate(activityDO.getEndTimeStr(), "yyyy-MM-dd HH:mm:ss");
        if(startTime.getTime() > endTime.getTime()){
            return ResultDTOBuilder.failure("1", "活动开始时间大于结束时间", null);
        }
        activityDO.setStartTime(startTime);
        activityDO.setEndTime(endTime);
        if(StringUtils.isEmpty(activityDO.getPosition())){
            return ResultDTOBuilder.failure("1", "请填写活动地点", null);
        }
        if(StringUtils.isEmpty(activityDO.getIntroduce())){
            return ResultDTOBuilder.failure("1", "请填写活动简介", null);
        }
        // 活动状态
        activityDO.setStatus(ActivityDO.STATUS_YFB);
        activityDO.setCreateTime(new Date());
        // 添加活动
        activityDAO.insert(activityDO);
        return ResultDTOBuilder.success(activityDO);
    }

    // 创建活动上传图片
    public ResultDTO uploadFile(MultipartFile[] lstFile, Integer userId, String handleType){
        if(lstFile == null || lstFile.length == 0){
            return ResultDTOBuilder.failure("1", "请选择上传文件", null);
        }
        try {
            List<String> lstFileUrl = new ArrayList<>();
            for(MultipartFile file : lstFile){
                String fileUrl = OSSUtils.updloadFile(file.getOriginalFilename(), file.getInputStream(), userId, handleType);
                lstFileUrl.add(fileUrl);
            }
            return ResultDTOBuilder.success(StringUtils.join(lstFileUrl, ","));
        } catch (Exception e) {
            return ResultDTOBuilder.failure("1", "上传图片有误", null);
        }
    }
}
